package cn.iocoder.yudao.module.erp.controller.admin.priwarehouseintegra;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.erp.controller.admin.priwarehouseintegra.vo.PriWarehouseIntegraPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.priwarehouseintegra.vo.PriWarehouseIntegraRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.priwarehouseintegra.vo.PriWarehouseIntegraSaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.priwarehouseintegra.PriWarehouseIntegraDO;
import cn.iocoder.yudao.module.erp.service.priwarehouseintegra.PriWarehouseIntegraService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "管理后台 - ERP 一级库整合单")
@RestController
@RequestMapping("/erp/pri-warehouse-integra")
@Validated
public class PriWarehouseIntegraController {

    @Resource
    private PriWarehouseIntegraService priWarehouseIntegraService;

    @PostMapping("/create")
    @Operation(summary = "创建ERP 一级库整合单")
    @PreAuthorize("@ss.hasPermission('erp:pri-warehouse-integra:create')")
    public CommonResult<Long> createPriWarehouseIntegra(@Valid @RequestBody PriWarehouseIntegraSaveReqVO createReqVO) {
        return success(priWarehouseIntegraService.createPriWarehouseIntegra(getLoginUserId(),createReqVO));
    }

    @PostMapping("/createPurPurchaseOrder")
    @Operation(summary = "创建ERP 一级库整合单")
    @PreAuthorize("@ss.hasPermission('erp:pri-warehouse-integra:create')")
    public CommonResult<Long> createPurPurchaseOrder(@Valid @RequestBody PriWarehouseIntegraSaveReqVO createReqVO) {
        return success(priWarehouseIntegraService.createPurPurchaseOrder(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新ERP 一级库整合单")
    @PreAuthorize("@ss.hasPermission('erp:pri-warehouse-integra:update')")
    public CommonResult<Boolean> updatePriWarehouseIntegra(@Valid @RequestBody PriWarehouseIntegraSaveReqVO updateReqVO) {
        priWarehouseIntegraService.updatePriWarehouseIntegra(updateReqVO);
        return success(true);
    }

    @PutMapping("/officeAudit")
    @Operation(summary = "更新ERP 一级库整合单")
    @PreAuthorize("@ss.hasPermission('erp:pri-warehouse-integra:update')")
    public CommonResult<Boolean> officeAudit(@Valid @RequestBody PriWarehouseIntegraSaveReqVO updateReqVO) {
        priWarehouseIntegraService.officeAudit(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除ERP 一级库整合单")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:pri-warehouse-integra:delete')")
    public CommonResult<Boolean> deletePriWarehouseIntegra(@RequestParam("id") Long id) {
        priWarehouseIntegraService.deletePriWarehouseIntegra(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得ERP 一级库整合单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:pri-warehouse-integra:query')")
    public CommonResult<PriWarehouseIntegraRespVO> getPriWarehouseIntegra(@RequestParam("id") Long id) {
        PriWarehouseIntegraDO priWarehouseIntegra = priWarehouseIntegraService.getPriWarehouseIntegra(id);
        return success(BeanUtils.toBean(priWarehouseIntegra, PriWarehouseIntegraRespVO.class));
    }

    @GetMapping("/submitOffice")
    @Operation(summary = "提交院办")
    @Parameter(name = "taskId", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:pri-warehouse-integra:query')")
    public CommonResult<Boolean> submitOffice(@RequestParam("taskId") String taskId) {
        priWarehouseIntegraService.submitOffice(taskId);
        return success(true);
    }

    @GetMapping("/page")
    @Operation(summary = "获得ERP 一级库整合单分页")
    @PreAuthorize("@ss.hasPermission('erp:pri-warehouse-integra:query')")
    public CommonResult<PageResult<PriWarehouseIntegraRespVO>> getPriWarehouseIntegraPage(@Valid PriWarehouseIntegraPageReqVO pageReqVO) {
        PageResult<PriWarehouseIntegraDO> pageResult = priWarehouseIntegraService.getPriWarehouseIntegraPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PriWarehouseIntegraRespVO.class));
    }

    @GetMapping("/page2")
    @Operation(summary = "获得ERP 一级库整合单分页")
    @PreAuthorize("@ss.hasPermission('erp:pri-warehouse-integra:query')")
    public CommonResult<PageResult<PriWarehouseIntegraRespVO>> getPriWarehouseIntegraPage2(@Valid PriWarehouseIntegraPageReqVO pageReqVO) {
        PageResult<PriWarehouseIntegraDO> pageResult = priWarehouseIntegraService.getPriWarehouseIntegraPage2(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PriWarehouseIntegraRespVO.class));
    }

    @GetMapping("/page3")
    @Operation(summary = "获得ERP 一级库整合单分页")
    @PreAuthorize("@ss.hasPermission('erp:pri-warehouse-integra:query')")
    public CommonResult<PageResult<PriWarehouseIntegraRespVO>> getPriWarehouseIntegraPage3(@Valid PriWarehouseIntegraPageReqVO pageReqVO) {
        PageResult<PriWarehouseIntegraDO> pageResult = priWarehouseIntegraService.getPriWarehouseIntegraPage3(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PriWarehouseIntegraRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出ERP 一级库整合单 Excel")
    @PreAuthorize("@ss.hasPermission('erp:pri-warehouse-integra:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPriWarehouseIntegraExcel(@Valid PriWarehouseIntegraPageReqVO pageReqVO,
                                               HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PriWarehouseIntegraDO> list = priWarehouseIntegraService.getPriWarehouseIntegraPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "ERP 一级库整合单.xls", "数据", PriWarehouseIntegraRespVO.class,
                BeanUtils.toBean(list, PriWarehouseIntegraRespVO.class));
    }

}